<script setup lang="ts">
import { ref } from 'vue';
import KnowledgeCard from '../components/KnowledgeCard.vue'
import type { Knowledge, KnowledgeList, KnowledgeParams } from '@/types/consult';
import { getKnowledgeList } from '@/apis/consult';
const props = defineProps<{
  type: Knowledge
}>()

const loading = ref(false)
const finished = ref(false)

const list = ref<KnowledgeList>([])
//定义查询参数响应式数据
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})
//List 组件通过 loading 和 finished 两个变量控制加载状态，当组件滚动到底部时，会触发 load 事件并将 loading 设置成 true。此时可以发起异步操作并更新数据，数据更新完毕后，将 loading 设置成 false 即可。若数据已全部加载完毕，则直接将 finished 设置成 true 即可。
const onLoad = async () => {
  const res = await getKnowledgeList(params.value)
  list.value.push(...res.data.rows)
  //加载下一页
  params.value.current++
  //加载状态结束
  loading.value = false
  //全部加载完成
  if (params.value.current > res.data.pageTotal) {
    finished.value = true
  }


}
</script>

<template>
  <div class="knowledge-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
